#!/bin/bash



# 2024年5月6日
url="https://gitee.com/okjack/okk/raw/master/li.txt"
# 2024年7月6日
url="https://gitee.com/sunboy0628/okk/raw/main/li.txt"


f1="/tmp/iptv_from_ok.txt"
f1_md5="$f1.md5"
f2="/tmp/iptv_from_ok.base64"

if [ "$FORCE" == "true" ];then
        rm "$1" "$2"
fi

#target_file="/root/web81/ok.live.txt"
# target_file="/home/hbl/services/fileserver/file_server_data/box1_more_and_more/ok.live.txt"
target_file="/home/hbl/services/f2/data/v3_tvbox_server/more_and_more/ok.live.txt"


echo "[i] bash _douyu.sh '$target_file.tmp' '$target_file' "



md5(){
        if [ ! -f "$1" ];then
                echo "--"
                return 1
        fi
        cat "$1" | md5sum
}

check(){
        if [ ! -f "$1" ];then
                return 1
        fi
        if [ "$(cat "$1" | wc -c)" -lt 200 ];then
                return 2
        fi
}

{

    curl -q "$url" -o "$f2"

    # echo "$(date '+%Y/%m/%d %H:%M:%S')"
    date '+%Y/%m/%d %H:%M:%S'

    if ! check "$f2";then
            echo "[-] check not ok"
            exit
    fi


    m1="$(cat "$f1_md5")"
    m2="$(md5 "$f2")"

    if [ "$m1" == "$m2" ];then
            echo "[-] md5 equal : $m1"
            exit 3
    fi


    ok=false
    if sed -E "s/.*\*\*//" "$f2" | base64 -d | tee "$f1.new" | grep "#genre#";then
            ok=true
    fi

    echo "isupdated=$ok"
} > /tmp/log.update_live_channels.log

# ok 是 git 有更新.
#
if [ "$ok" == "true" ] || [ "$FORCE" == "true" ];then
        #mv "$f1.new" "$f1"
        mv "$f1.new" "$target_file.tmp"
        echo "$m2" > "$f1_md5"

        bash  ____add_more_channel_to_live.sh "$target_file.tmp" "$target_file"

fi

